package com.se7en.ufutil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.RatingBar;
import android.widget.Toast;

public class CommentForm extends Activity {

	float overall;
	float quiet;
	float crowd;
	Intent intent;
	String studyLocation;
	int locationID;
	String input;
	String deviceID;
	
	public void onCreate(Bundle savedInstanceState) {
		intent = getIntent();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.commentform);
		studyLocation = intent.getStringExtra("name");
		locationID = intent.getIntExtra("id", 1);
		deviceID = intent.getStringExtra("deviceID");
		TextView tv = (TextView) findViewById(R.id.formTitle);
		tv.setText(studyLocation);
		final EditText et = (EditText) findViewById(R.id.commentinput);
		RatingBar orsubmit = (RatingBar) findViewById(R.id.submitoverallrating);
		RatingBar qsubmit = (RatingBar) findViewById(R.id.submitquietrating);
		RatingBar csubmit = (RatingBar) findViewById(R.id.submitcrowdedrating);
		Button r = (Button) findViewById(R.id.submitrating);
		
		Button b = (Button) findViewById(R.id.commentsubmit);
		b.setOnClickListener(new View.OnClickListener() {
		
		
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),StudyDisplay.class);
				intent.putExtra("name", studyLocation);
				input = et.getText().toString();
				DataAccess.postComment(locationID, input, deviceID);
				v.getContext().startActivity(intent);
				
			}
		});
		
		orsubmit.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar rb, float rating, boolean fromUser) {
				overall = rating;
				//Toast.makeText(CommentForm.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
			}
			
			
		});
		
		qsubmit.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar rb, float rating, boolean fromUser) {
				quiet = rating;
				//Toast.makeText(CommentForm.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
			}
			
			
		});
		
		csubmit.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar rb, float rating, boolean fromUser) {
				crowd = rating;
				//Toast.makeText(CommentForm.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
			}
			
			
		});
		
		
		r.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),StudyDisplay.class);
				intent.putExtra("name", studyLocation);
				//Toast.makeText(CommentForm.this, "Rating : " + overall + "  " + quiet + "  " + crowd,Toast.LENGTH_LONG).show();
				DataAccess.postRating(locationID,deviceID,overall,quiet,crowd);
				v.getContext().startActivity(intent);
				
			}
		});
	
	}
}
